package com.droidlogic.mboxlauncher;

import android.animation.Animator;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class CustomView extends FrameLayout implements AdapterView.OnItemClickListener, ViewTreeObserver.OnGlobalLayoutListener {
    /* access modifiers changed from: private */
    public static boolean allowAnimation = true;
    static final Object mLock = new Object[0];
    private GridView gv = null;
    private int homeShortcutCount;
    private ImageView img_dim = null;
    private ImageView img_screen_shot = null;
    private ImageView img_screen_shot_keep = null;
    /* access modifiers changed from: private */
    public Context mContext = null;
    private int mMode = -1;
    private View mSource;
    private String str_custom_apps;
    /* access modifiers changed from: private */
    public CustomView thisView = null;
    private int transY = 0;

    public CustomView(Context context, View source, int mode) {
        super(context);
        this.mContext = context;
        this.mSource = source;
        this.mMode = mode;
        initLayout();
    }

    private void initLayout() {
        Log.e("Launcher","CustomView initLayout>>");
        inflate(this.mContext, R.layout.layout_custom, this);
        this.thisView = this;
        this.gv = (GridView) findViewById(R.id.grid_add_apps);
        gv.setBackground(mContext.getDrawable(R.drawable.bg_add_apps));
        this.gv.setOnItemClickListener(this);
        displayView();
        this.str_custom_apps = ((Launcher) this.mContext).getAppDataLoader().getShortcutString(this.mMode);
        getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    public void onGlobalLayout() {
        Log.e("Launcher","CustomView onGlobalLayout>>");
        getViewTreeObserver().removeGlobalOnLayoutListener(this);
        Rect rect = new Rect();
        this.mSource.getGlobalVisibleRect(rect);
        if (rect.top > this.mContext.getResources().getDisplayMetrics().heightPixels / 2) {
            this.transY = -getHeight();
        } else {
            this.transY = getHeight();
        }
        setCustomView();

    }

    public void setCustomView() {
        Log.e("Launcher","CustomView setCustomView>>");
        ((Launcher) this.mContext).getMainView().setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        ((Launcher) this.mContext).getMainView().animate().translationY((float) this.transY).setDuration(300).alpha(0.5f).start();
        this.gv.getGlobalVisibleRect(new Rect());
        this.gv.requestFocus();
        TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, (float) (-this.transY), 0.0f);
        translateAnimation.setDuration(300);
        translateAnimation.setInterpolator(new AccelerateInterpolator());
        this.gv.startAnimation(translateAnimation);
    }

    public void recoverMainView() {
        Launcher context = (Launcher) this.mContext;
        View view = context.getMainView();
        setVisibility(VISIBLE);
        if (allowAnimation) {
            allowAnimation = false;
            TranslateAnimation translateAnimation = new TranslateAnimation(0.0f, 0.0f, 0.0f, (float) (-this.transY));
            translateAnimation.setDuration(300);
            translateAnimation.setInterpolator(new AccelerateInterpolator());
            this.gv.startAnimation(translateAnimation);
            view.animate()
                    .translationY(0.0f)
                    .setDuration(300)
                    .alpha(1.0f)
                    .setInterpolator(new AccelerateInterpolator())
                    .setListener(new mAnimatorListener())
                    .start();
            context.getAppDataLoader().saveShortcut(this.mMode, this.str_custom_apps);
        }
    }

    public List<ArrayMap<String, Object>> getAppList() {
        Log.e("Launcher","getAppList>>");
        List<ArrayMap<String, Object>> appList = new ArrayList<>();
        List<ArrayMap<String, Object>> list_all = ((Launcher) this.mContext).getAppDataLoader().getShortcutList(4);
        List<ArrayMap<String, Object>> list_current = ((Launcher) this.mContext).getAppDataLoader().getShortcutList(this.mMode);
        this.homeShortcutCount = 0;
        Log.e("Launcher","getAppList>>list_all:"+list_all.size());
        for (int i = 0; i < list_all.size(); i++) {
            ArrayMap<String, Object> map = new ArrayMap<>();
            map.put("item_selection", R.drawable.item_img_unsel);
            int j = 0;
            while (true) {
                if (j >= list_current.size() - 1) {
//                    Log.e("Launcher","getAppList>>"+j);
                    break;
                } else if(TextUtils.equals(list_all.get(i).get("component name").toString(),
                        list_current.get(j).get("component name").toString()))
                {
//                    Log.e("Launcher","getAppList>>"+j+":"+i);
                    map.put("item_selection", R.drawable.item_img_sel);
                    if (this.mMode == 0) {
                        this.homeShortcutCount++;
                    }
                    j++;
                }else {
                    j++;
                }
            }
            map.put("item_name", list_all.get(i).get("name"));
            map.put("item_icon", list_all.get(i).get("icon"));
            map.put("item_background", parseItemBackground(i));
            map.put("component name", list_all.get(i).get("component name"));
            appList.add(map);
        }
        Log.e("Launcher","appList="+appList.size());
        return appList;
    }

    public void displayView() {
        Log.e("Launcher","CustomView displayView");
        this.gv.setAdapter(new LocalAdapter(this.mContext, getAppList(),
                R.layout.add_apps_grid_item,
                new String[]{"item_icon", "item_name", "item_selection", "item_background"},
                new int[]{R.id.item_type, R.id.item_name, R.id.item_sel, R.id.relative_layout}));
    }

    private void updateView() {
        ((BaseAdapter) this.gv.getAdapter()).notifyDataSetChanged();
    }

    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getAction() == 0) {
            switch (event.getKeyCode()) {
                case 4:
                    recoverMainView();
                    break;
            }
        }
        return super.dispatchKeyEvent(event);
    }

    private int parseItemBackground(int num) {
        switch ((num % 20) + 1) {
            case 0:
                return R.drawable.item_child_3;
            case 1:
                return R.drawable.item_child_1;
            case 2:
                return R.drawable.item_child_2;
            case 3:
                return R.drawable.item_child_3;
            case 4:
                return R.drawable.item_child_4;
            case 5:
                return R.drawable.item_child_5;
            case 6:
                return R.drawable.item_child_6;
            case 7:
                return R.drawable.item_child_3;
            case 8:
                return R.drawable.item_child_4;
            case 9:
                return R.drawable.item_child_1;
            case 10:
                return R.drawable.item_child_2;
            case 11:
                return R.drawable.item_child_6;
            case 12:
                return R.drawable.item_child_5;
            case 13:
                return R.drawable.item_child_6;
            case 14:
                return R.drawable.item_child_2;
            case 15:
                return R.drawable.item_child_5;
            case 16:
                return R.drawable.item_child_3;
            case 17:
                return R.drawable.item_child_1;
            case 18:
                return R.drawable.item_child_4;
            case 19:
                return R.drawable.item_child_2;
            default:
                return R.drawable.item_child_1;
        }
    }
    @Override
    public void onItemClick(android.widget.AdapterView<?> adapterView, android.view.View view, int position, long r10) {
        synchronized (mLock){
            Log.d("MediaBoxLauncher","onItemClick>>");
            ArrayMap items = (ArrayMap)adapterView.getItemAtPosition(position);
            int item = (int) items.get("item_selection");
            ComponentName componentName = (ComponentName) items.get("component name");
            String packageName = componentName.getPackageName();
            StringBuilder sb = new StringBuilder();
            int idRes = R.drawable.item_img_unsel;
            if (item == R.drawable.item_img_unsel){
                if (mMode == 0 && homeShortcutCount >= Launcher.HOME_SHORTCUT_COUNT){
                    Toast.makeText(mContext,mContext.getString(R.string.str_nospace),Toast.LENGTH_SHORT).show();
                    return;
                }
                str_custom_apps = sb.append(str_custom_apps).append(packageName).append(";").toString();
                idRes = R.drawable.item_img_sel;
                homeShortcutCount++;
            }else if(item == R.drawable.item_img_sel){
                String str = sb.append(packageName).append(";").toString();
                str_custom_apps = str_custom_apps.replaceAll(str,"");
                idRes = R.drawable.item_img_unsel;
                homeShortcutCount--;
            }
            items.put("item_selection",idRes);
            updateView();
        }

    }

    private class mAnimatorListener implements Animator.AnimatorListener {

        @Override
        public void onAnimationCancel(Animator animation) {
        }
        @Override
        public void onAnimationStart(Animator animation) {
        }
        @Override
        public void onAnimationEnd(Animator animation) {
            boolean unused = CustomView.allowAnimation = true;
            ((Launcher) CustomView.this.mContext).getRootView().removeView(CustomView.this.thisView);
            ((Launcher) CustomView.this.mContext).getMainView().animate().setListener((Animator.AnimatorListener) null);
            ((Launcher) CustomView.this.mContext).recoverFromCustom();
        }
        @Override
        public void onAnimationRepeat(Animator animation) {
        }
    }
}